/* 
 * File:   main.cpp
 * Author: dima
 *
 * Created on 20 Январь 2015 г., 16:09
 */

#include <cstdlib>

using namespace std;

/*
 * 
 */


#include <iostream>
#include "ipd_png.h"
#include "ipd_img.h"
#include "ipd_exception.h"
#include "ipd_scaling_filter.h"
#include "ipd_scaling.h"

std::string file_path1 = "1.png";
std::string file_path2 = "mask.png";
std::string file_path3 = "bilinear_interpolation.png";
std::string file_path4 = "duplicating_pixels.png";
std::string file_path5 = "2_1.png";

void scale_decrease(ipd::ipd_png *png, int sc)
{
    unsigned char* ch;
    int i, j, k, p;
    ipd::ipd_img *img1 = new ipd::ipd_img();
    ipd::ipd_img *original_image = new ipd::ipd_img();
    try
    {
        png->load_png(*original_image, file_path1);
        ipd::color_model model = original_image->get_color_type();
        int w_img = original_image->get_width() / sc;
        int h_img = original_image->get_height() / sc;
        img1->init(w_img, h_img, model);
        for (i = 0; i < w_img; i++)
            for (j = 0; j < h_img; j++)
            {
                ch = original_image->get_color(i * sc, j * sc);
                img1->set_color(i, j, ch);
            }
    }
    catch (ipd::ipd_exception &except)
    {
        cout << "File " << except.in_file() << " Message" << except.what() << '\n';
    }
    png->save_png(*img1, file_path4);
    delete original_image;
    delete img1;
    cout << "Excellent" << '\n';
}

void scale_increase(ipd::ipd_png *png, int sc)
{
    unsigned char* ch;
    int i, j, k, p;
    ipd::ipd_img *img1 = new ipd::ipd_img();
    ipd::ipd_img *original_image = new ipd::ipd_img();
    try
    {
        png->load_png(*original_image, file_path1);
        ipd::color_model model = original_image->get_color_type();
        int w_img = original_image->get_width() * sc;
        int h_img = original_image->get_height() * sc;
        img1->init(w_img, h_img, model);
        for (i = 0; i < original_image->get_width(); i++)
        {
            for (j = 0; j < original_image->get_height(); j++)
            {
                ch = original_image->get_color(i, j);
                for (k = 0; k < sc; k++)
                    for (p = 0; p < sc; p++)
                        img1->set_color(i * sc + k, j * sc + p, ch);
            }
        }
    }
    catch (ipd::ipd_exception &except)
    {
        cout << "File " << except.in_file() << " Message" << except.what() << '\n';
    }
    png->save_png(*img1, file_path3);
    delete original_image;
    delete img1;
    cout << "Excellent" << '\n';
}

int main(int argc, char** argv)
{

    ipd::ipd_png *png = new ipd::ipd_png();

    ipd::ipd_img *img1 = new ipd::ipd_img();
    ipd::ipd_img *img2 = new ipd::ipd_img();

    ipd::ipd_scaling_filter *scaling1 = new ipd::ipd_scaling_filter(*img1, 5);
    ipd::ipd_scaling *scaling2 = new ipd::ipd_scaling(*img1);
    try
    {
        png->load_png(*img1, file_path1);
        
        scaling1->init();
        scaling1->convert_image(*img2);        
        png->save_png(*img2, file_path2);
        
        scaling2->bilinear_interpolation(*img2,3);
        png->save_png(*img2, file_path3);
        
        scaling2->duplicating_pixels(*img2,3,true);
        png->save_png(*img2, file_path4);
    }
    catch (ipd::ipd_exception &except)
    {
        cout << "File " << except.in_file() << " Message" << except.what() << '\n';
    }
    delete scaling1;
    delete scaling2;
    delete img2;
    delete img1;
    delete png;
    cout << "Excellent" << '\n';
    return 0;
}

/*
double mean(double *mass,int sz)
{
    int a;
    for (int i = 0; i < sz; i++)
    {
        a=0;
        for (int j = 0; j < sz; j++)
        {
            if(mass[i]<mass[j]||(mass[i]==mass[j]&& i<j) )
                a++;
        }
        if(a==(sz-1)/2)
            return mass[i];
    }
}
 */